package fhq.hcmute.qrcode;

import fhq.hcmute.qrcode.common.BitMatrix;
import fhq.hcmute.qrcode.common.DataMode;
import fhq.hcmute.qrcode.common.ErrorCorrectionLevel;
import fhq.hcmute.qrcode.encoder.Encoder;
import fhq.hcmute.qrcode.util.QRCodeException;

/**
 * @author Hien Nguyen Le
 * @Email: nglehien@gmail.com
 */
public class QRCodeWriter {
	private QRCode qrCode;

	public QRCodeWriter(String contents, ErrorCorrectionLevel level)
			throws QRCodeException {
		this(contents, level, Encoder.NOT_SELECTED_VERSION);
	}

	public QRCodeWriter(String contents, ErrorCorrectionLevel level, int version)
			throws QRCodeException {
		DataMode mode = DataMode.chooseMode(contents);
		Encoder encoder = null;
		if (version == Encoder.NOT_SELECTED_VERSION) {
			encoder = new Encoder(mode, level, contents);
		} else {
			encoder = new Encoder(mode, level, contents, version);
		}
		encoder.appendHeader();
		encoder.appendContents();
		encoder.terminateBits();
		encoder.appendSpecialBitStrings();
		encoder.createErrorCorrectionCodeWords();
		qrCode = encoder.chooseBestQRCode();

	}

	public BitMatrix getDataMatrix() {
		return qrCode.getDataMatrix();
	}

	public void printQRCodeInfo() {
		System.out.println(qrCode.toString());
	}

	public QRCode getQrCode() {
		return qrCode;
	}

}
